package com.wenqiner.requesttool.domain;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.wenqiner.requesttool.core.constants.FileConstant;
import com.wenqiner.requesttool.util.ParsePlaceholderUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author rhf
 * @date 2023-03-30
 **/
public class OriginRequestJsonData {
    public static final Logger log = LoggerFactory.getLogger(OriginRequestJsonData.class);

    /**
     * 请求头
     */
    private Map<String, String> headerMap;
    /**
     * 请求体
     */
    private String body;

    /**
     * 行数据
     */
    private List<Map<String, String>> rowData;

    public Map<String, String> getHeaderMap() {
        return headerMap;
    }

    public void setHeaderMap(Map<String, String> headerMap) {
        this.headerMap = headerMap;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public List<Map<String, String>> getRowData() {
        return rowData;
    }

    public void setRowData(List<Map<String, String>> rowData) {
        this.rowData = rowData;
    }

    /**
     * @param index 序号
     * @return com.wenqiner.requesttool.domain.RequestJsonData.RequestData
     * @author rhf
     */
    public RequestData acquire(Integer index) {
        RequestData requestData = new RequestData();
        if (CollUtil.isEmpty(rowData)) {
            String s = null;
            //克隆一个可修改的headerMap
            Map<String, String> curMap = new HashMap<>(headerMap.size());
            curMap.putAll(headerMap);
            try {
                s = ParsePlaceholderUtil.parseMethodOuter(body);
                curMap.keySet()
                        .forEach(key -> {
                            if (null != curMap.get(key)) {
                                try {
                                    curMap.put(key, ParsePlaceholderUtil.parseMethodOuter(curMap.get(key)));
                                } catch (Exception ignored) {
                                }
                            }
                        });
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            requestData.setBody(s);
            requestData.setHeaderMap(curMap);
        } else {
            //超过行数据的上取模
            if (index > rowData.size()) {
                index = index % rowData.size();
            }
            Map<String, String> map = rowData.get(index);
            String s = body;
            //克隆一个可修改的headerMap
            Map<String, String> curMap = new HashMap<>(headerMap.size());
            curMap.putAll(headerMap);
            for (String key : map.keySet()) {
                String field = FileConstant.PARAM_PREFIX.concat(key).concat(FileConstant.PARAM_SUFFIX);
                s = s.replace(field, map.get(key));
                //替换map中的值
                if (MapUtil.isNotEmpty(curMap)) {
                    curMap.keySet().forEach(k -> {
                        String v = curMap.get(k);
                        if (CharSequenceUtil.isNotBlank(v)) {
                            v = v.replace(field, map.get(key));
                            try {
                                v = ParsePlaceholderUtil.parseMethodOuter(v);
                            } catch (Exception ignored) {
                            }
                            curMap.put(k, v);
                        }
                    });
                }
            }
            requestData.setHeaderMap(curMap);
            try {
                s = ParsePlaceholderUtil.parseMethodOuter(s);
            } catch (Exception ignored) {
            }
            requestData.setBody(s);
        }
        log.info("请求参数转换，转换前参数[{}],转换前header[{}],\n转换后参数[{}],转换后header[{}]"
                , body, headerMap
                , requestData.getBody(), requestData.getHeaderMap());
        return requestData;
    }

    public static class RequestData {
        /**
         * 请求头
         */
        private Map<String, String> headerMap;
        /**
         * 请求体
         */
        private String body;

        public Map<String, String> getHeaderMap() {
            return headerMap;
        }

        public void setHeaderMap(Map<String, String> headerMap) {
            this.headerMap = headerMap;
        }

        public String getBody() {
            return body;
        }

        public void setBody(String body) {
            this.body = body;
        }
    }

}
